package cn.edu.zjicm.wordsnet_d.adapter.common;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import cn.edu.zjicm.wordsnet_d.data.metadata.ChildItem;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

public abstract class BaseGroupAdapter<T extends Comparable<T>,T2 extends ChildItem<T>> extends	BaseExpandableListAdapter {
	Context mContext;
	SortedMap<T, List<T2>> mDateMap;

	public BaseGroupAdapter(Context context) {
		this(context, null, null);
	}
	
	public BaseGroupAdapter(Context context, List<T2> list){
		this(context, null ,list);
	}
	
	public BaseGroupAdapter(Context context, Comparator<T> comparator) {
		this(context, comparator, null);
	}

	public BaseGroupAdapter(Context context,Comparator<T> comparator, List<T2> list) {
		// TODO Auto-generated constructor stub
		mContext = context;
		if(comparator != null){
			mDateMap = new TreeMap<T, List<T2>>(comparator);//设定排序
		}else{
			mDateMap = new TreeMap<T, List<T2>>();
		}
		if (list != null) {
			initGroupInfo(list);
		}
	}
	
	public ArrayList<T2> getValueList(){
		ArrayList<T2> list = new ArrayList<T2>();
		Iterator<List<T2>> it = mDateMap.values().iterator();
		while(it.hasNext()){
			list.addAll(it.next());
		}
		return list;
	}

	public void addItem(T2 childItem) {
		if (childItem == null)
			return;
		T groupBy = childItem.getGroupBy();
		if (mDateMap.containsKey(groupBy) && mDateMap.get(groupBy) != null) {
			List<T2> sortedlist = mDateMap.get(groupBy);
			sortedlist.add(0, childItem);
			//Collections.sort(sortedlist);
		} else {
			List<T2> sortedList = new ArrayList<T2>();
			sortedList.add(childItem);
			mDateMap.put(groupBy, sortedList);
		}
		notifyDataSetChanged();
	}
	
	public void removeItem(T2 childItem) {
		if (childItem == null)
			return;
		boolean success = false;
		T groupBy = childItem.getGroupBy();
		if (mDateMap.containsKey(groupBy) && mDateMap.get(groupBy) != null) {
			List<T2> sortedlist = mDateMap.get(groupBy);
			success = sortedlist.remove(childItem);
			//add by zhengjun 如果一个group中的所有元素都删除后，这个group也要删除
			if(sortedlist.size()==0)
				mDateMap.remove(groupBy);
		}
		if (success) {
			notifyDataSetChanged();
		} else {
			Log.d("DateGroupAdapter.removeUnit", "unit Not Fount");
		}
	}

	private void initGroupInfo(List<T2> itemList) {
		for (int i = 0; i < itemList.size(); i++) {
			T2 u = itemList.get(i);
			T groupItem = u.getGroupBy();
			if (!mDateMap.containsKey(groupItem) || mDateMap.get(groupItem) == null) {
				List<T2> sortedList = new ArrayList<T2>();
				sortedList.add(u);
				mDateMap.put(groupItem, sortedList);
			} else {
				List<T2> sortedList = mDateMap.get(groupItem);
				sortedList.add(u);
				mDateMap.put(groupItem, sortedList);
			}
		}
	}
	
	public void setList(List<T2> list){
		mDateMap.clear();
		initGroupInfo(list);
		notifyDataSetChanged();
	}

	public T2 getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return getGroup(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return getCombinedChildId(groupPosition, childPosition);
	}

	public abstract View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent);

	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		List<T2> list = getGroup(groupPosition);
		return list.size();
	}

	public List<T2> getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return (List<T2>) mDateMap.values().toArray()[groupPosition];
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return mDateMap.size();
	}

	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return getCombinedGroupId(groupPosition);
	}
	
	public int getRealPosition(int groupPosition, int childPosition){
		int position = 0;
		Iterator<List<T2>> it = mDateMap.values().iterator();
		for(int i = 0; i < groupPosition; i++){
			position += it.next().size();
		}
		position += childPosition;
		return position;
	}

	public abstract View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent);

	public abstract boolean hasStableIds();

	public abstract boolean isChildSelectable(int groupPosition, int childPosition);

}